<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form ref="dialogForm" :model="form" :rules="rules" label-width="100px" label-position="left">
			<el-form-item label="排序号">
				<el-input-number v-model="form.sortNo" placeholder="=排序号"></el-input-number>
			</el-form-item>
			<el-form-item label="表单名称" prop="formTitle">
				<el-input v-model="form.formTitle" placeholder="表单名称"></el-input>
			</el-form-item>
			<el-form-item label="数据库表名" prop="tableName">
				<el-input v-model="form.tableName" placeholder="数据库表名" :disabled="true"></el-input>
			</el-form-item>
			<el-form-item label="日志状态">
				<el-switch v-model="form.dataLogFlag" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注">
				<el-input v-model="form.remark" maxlength="150" placeholder="备注"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
			<el-button @click="visible=false">取消</el-button>
		</template>
	</el-dialog>
</template>

<script>
import sysConfig from "@/config";
import {generateId} from "@/utils/util"
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: '新增办公单',
				edit: '编辑办公单'
			},
			form: {
				sortNo: 0,
				formId: "",
				formTitle: "",
				formType: "0",
				tableName: generateId(),
				model:"1",
				dataLogFlag: "",
				remark: "",
			},
			visible: false,
			isSaveing: false,
			rules: {
				formTitle: [
					{required: true, message: '表单名称不能为空', trigger: 'blur'}
				],
				formType: [
					{required: true, message: '表单类型不能为空', trigger: 'blur'}
				],
				tableName: [
					{required: true, message: '数据库表名称不能为空', trigger: 'blur'}
				]
			},
		}
	},
	created() {
		if(sysConfig.BPM_FIELD)
		{
			//this.form.tableName =
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.bpm.bpmForm.insert.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.bpm.bpmForm.update.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.formId = data.formId
			this.form.sortNo = data.sortNo;
			this.form.formTitle = data.formTitle;
			this.form.formType = data.formType;
			this.form.tableName = data.tableName;
			this.form.model = data.model;
			this.form.dataLogFlag = data.dataLogFlag
			this.form.remark = data.remark
		}
	}
}
</script>

<style>
</style>
